package com.devtools.project.tool.module.controller;

import java.util.Arrays;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.devtools.framework.aspectj.lang.annotation.Log;
import com.devtools.framework.aspectj.lang.enums.BusinessType;
import com.devtools.project.tool.module.domain.Module;
import com.devtools.project.tool.module.service.IModuleService;
import com.devtools.framework.web.controller.BaseController;
import com.devtools.framework.web.page.TableDataInfo;
import com.devtools.framework.web.domain.AjaxResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.devtools.common.utils.text.Convert;
import org.springframework.stereotype.Controller;
import com.devtools.common.utils.poi.ExcelUtil;

/**
 * 模块信息操作处理
 * 
 * @author devtools
 * @date 2022-11-28
 */
@Controller
@RequestMapping("/tool/module")
public class ModuleController extends BaseController
{
    private String prefix = "tool/module";
	
	@Autowired
	private IModuleService moduleService;
	
	@RequiresPermissions("tool:module:view")
	@GetMapping()
	public String module()
	{
	    return prefix + "/module";
	}
	
	/**
	 * 查询模块列表
	 */
	@RequiresPermissions("tool:module:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Module module)
	{
		startPage();
        QueryWrapper<Module> queryWrapper = new QueryWrapper<Module>(module);
		List<Module> list = moduleService.list(queryWrapper);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出模块列表
	 */
	@RequiresPermissions("tool:module:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Module module)
    {
    	QueryWrapper<Module> queryWrapper = new QueryWrapper<Module>(module);
		List<Module> list = moduleService.list(queryWrapper);
        ExcelUtil<Module> util = new ExcelUtil<Module>(Module.class);
        return util.exportExcel(list, "module");
    }
	
	/**
	 * 新增模块
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存模块
	 */
	@RequiresPermissions("tool:module:add")
	@Log(title = "模块", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Module module)
	{		
		return toAjax(moduleService.save(module) ? 1 : 0);
	}

	/**
	 * 修改模块
	 */
	@GetMapping("/edit/{moduleId}")
	public String edit(@PathVariable("moduleId") Integer moduleId, ModelMap mmap)
	{
		Module module = moduleService.getById(moduleId);
		mmap.put("module", module);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存模块
	 */
	@RequiresPermissions("tool:module:edit")
	@Log(title = "模块", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Module module)
	{	
		return toAjax(moduleService.updateById(module)? 1 : 0);
	}
	
	/**
	 * 删除模块
	 */
	@RequiresPermissions("tool:module:remove")
	@Log(title = "模块", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{	
		List<Integer> lst = Arrays.asList(Convert.toIntArray(ids));	
		return toAjax(moduleService.removeByIds(lst)? 1 : 0);
	}
	
}
